<?php

/**
 * This is the model class for table "tbl_pesanan".
 *
 * The followings are the available columns in table 'tbl_pesanan':
 * @property string $ID_Pesanan
 * @property string $Jam_Keberangkatan
 * @property string $Tgl_Pesanan
 * @property string $Asal
 * @property string $Tujuan
 * @property integer $No_tempat_duduk
 * @property string $Username
 *
 * The followings are the available model relations:
 * @property TblPelanggan $username
 * @property TblPembayaran[] $tblPembayarans
 */
class Pesanan extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Pesanan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_pesanan';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ID_Pesanan, Username, Tgl_Pesanan', 'required'),
			array('No_tempat_duduk', 'numerical', 'integerOnly'=>true),
			array('ID_Pesanan', 'length', 'max'=>20),
			array('Asal, Tujuan, Username', 'length', 'max'=>10),
			array('Jam_Keberangkatan, Tgl_Pesanan', 'safe'),
                        array('Tgl_Pesanan', 'date', 'format'=>'dd-mm-yyyy'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID_Pesanan, Jam_Keberangkatan, Tgl_Pesanan, Asal, Tujuan, No_tempat_duduk, Username', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'username' => array(self::BELONGS_TO, 'TblPelanggan', 'Username'),
			'tblPembayarans' => array(self::MANY_MANY, 'TblPembayaran', 'tbl_pesanan_pembayaran(ID_Pesanan, ID_Pembayaran)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID_Pesanan' => 'ID Pesanan',
			'Jam_Keberangkatan' => 'Jam Keberangkatan',
			'Tgl_Pesanan' => 'Tanggal Pesanan',
			'Asal' => 'Asal',
			'Tujuan' => 'Tujuan',
			'No_tempat_duduk' => 'No Tempat Duduk',
			'Username' => 'Username',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID_Pesanan',$this->ID_Pesanan,true);
		$criteria->compare('Jam_Keberangkatan',$this->Jam_Keberangkatan,true);
		$criteria->compare('Tgl_Pesanan',$this->Tgl_Pesanan,true);
		$criteria->compare('Asal',$this->Asal,true);
		$criteria->compare('Tujuan',$this->Tujuan,true);
		$criteria->compare('No_tempat_duduk',$this->No_tempat_duduk);
		$criteria->compare('Username',$this->Username,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

        public function getAsal(){
            return array('Bandung'=>'Bandung', 'Pamulang'=>'Pamulang', 'Muncul'=>'Muncul');
        }

        public function getTujuan($Asal='Bandung'){
            if($Asal=='Bandung'){
                return array('Pamulang'=>'Pamulang', 'Muncul'=>'Muncul');
            }else if($Asal=='Pamulang' || $Asal=='Muncul'){
                return array('Bandung'=>'Bandung');
            }
        }

        public function getTempatDuduk($Tgl=null,$Asal=null,$Tujuan=null,$Jam=null){
            if($Tgl!=null && $Asal!=null && $Tujuan!=null && $Jam!=null){
                $pesanans = Pesanan::model();
                $array = array();
                for($i = 1; $i <= 7; $i++){
                    if(!$pesanans->exists($condition = 'Tgl_Pesanan = :Tgl and Asal = :Asal and Tujuan = :Tujuan and Jam_Keberangkatan = :Jam and No_Tempat_duduk = :no',
                            $param = array(':Tgl'=>$Tgl,':Asal'=>$Asal, ':Tujuan'=>$Tujuan, ':Jam'=>$Jam, ':no'=>$i) ))
                                $array[$i] = $i;
                }
                return $array;

            }else{
                return array( 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7);
            }
        }

        public function getJamKeberangkatan(){
            return array('6:00'=>'6:00','8:00'=>'8:00','10:00'=>'10:00','16:00'=>'16:00','18:00'=>'18:00','20:00'=>'20:00');
        }
}